'use strict';

const Controller = require('egg').Controller;

class ClassifyController extends Controller {
  //创建分类(管理员 需要token)
  /**
     * 
     * @api {POST} /api/classify/create 管理台-创建分类(管理员 需要token)
     * @apiName 创建分类
     * @apiGroup 分类模块
     * @apiVersion  1.0.0
     * 
     * 
     * @apiParam  {String} classifyName 分类名称
     * @apiParam  {Number} projectId 项目id
     * 
     * @apiParamExample  {type} Request-Example:
        {
            "classifyName":"abc",
            "projectId":4
        }
     * 
     * 
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "id": 10,                       //分类id
                "classifyName": "abc",          //分类名
                "projectId": "4",               //项目id
                "updatedAt": "2021-10-12T08:58:51.865Z",    //最后更新时间
                "createdAt": "2021-10-12T08:58:51.865Z"     //创建时间
            }
        }
     * 
     * 
     */
  async create() {
    const { app, ctx, service } = this;
    let checkRes = new ctx.helper.checkData(this, 'classifyName', 'projectId')
      .checkDataRes;

    try {
      if (checkRes) {
        let res = await service.blog.classify.create();
        if (res == '您不是管理员') {
          return ctx.helper.errorModule(res);
        }
        if (res == '项目id错误') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('操作失败');
    }
  }
  //删除分类
  /**
     * 
     * @api {DELETE} /api/classify/delete 管理台-删除分类(管理员 需要token)
     * @apiName 删除分类
     * @apiGroup 分类模块
     * @apiVersion  1.0.0
     * 
     * 
     * @apiParam  {Number} id 分类id
     * @apiParam  {Number} projectId 项目id
     * 
     * 
     * 
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "删除成功",
            "data": []
        }
     * 
     * 
     */
  async delete() {
    const { app, ctx, service } = this;
    try {
      let res = await service.blog.classify.delete();
      if (res == '您不是管理员') {
        return ctx.helper.errorModule(res);
      }
      if (res == '项目id错误') {
        return ctx.helper.errorModule(res);
      }
      return ctx.helper.successModule('删除成功');
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('删除失败');
    }
  }
  /**
     * 
     * @api {GET} /api/classify/get?projectId=4 客户端-获取分类信息
     * @apiName 获取分类信息
     * @apiGroup 分类模块
     * @apiVersion  1.0.0
     * 
     * @apiParam  {Number} projectId 项目id
     * 
     * 
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "count": 4,     //分类总数
                "rows": [
                    {
                        "id": 11,       //分类id
                        "classifyName": "HTML", //分类名
                        "projectId": 4,         //项目id
                        "createdAt": "2021-10-12T09:11:23.000Z",    //分类创建时间
                        "updatedAt": "2021-10-12T09:21:18.000Z",    //分类最后修改时间
                        "deletedAt": null
                    },
                    {
                        "id": 12,
                        "classifyName": "CSS",
                        "projectId": 4,
                        "createdAt": "2021-10-12T09:21:27.000Z",
                        "updatedAt": "2021-10-12T09:21:27.000Z",
                        "deletedAt": null
                    },
                    {
                        "id": 13,
                        "classifyName": "JS",
                        "projectId": 4,
                        "createdAt": "2021-10-12T09:21:32.000Z",
                        "updatedAt": "2021-10-12T09:21:32.000Z",
                        "deletedAt": null
                    },
                    {
                        "id": 14,
                        "classifyName": "node.js",
                        "projectId": 4,
                        "createdAt": "2021-10-12T09:21:42.000Z",
                        "updatedAt": "2021-10-12T09:21:42.000Z",
                        "deletedAt": null
                    }
                ]
            }
        }
     * 
     * 
     */
  async get() {
    const { app, ctx, service } = this;
    const checkRes = new ctx.helper.checkData(this, 'projectId').checkDataRes;

    try {
      if (checkRes) {
        let res = await service.blog.classify.get();
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule(error);
    }
  }
  /**
     * 
     * @api {PUT} /api/classify/update 管理台-修改分类(管理员 需要token)
     * @apiName 修改分类
     * @apiGroup 分类模块
     * @apiVersion  1.0.0
     * 
     * @apiParam  {Number} id 分类id
     * @apiParam  {Number} projectId 项目id
     * @apiParam  {String} classifyName 分类名称
     * 
     * @apiParamExample  {type} Request-Example:
        {
            "id":11,
            "projectId":4
            "classifyName":"PHP"
        }
    * 
    * 
    * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "修改成功",
            "data": []
        }
    * 
    * 
    */
  async update() {
    const { app, ctx, service } = this;
    let checkRes = new this.ctx.helper.checkData(this, 'id', 'classifyName')
      .checkDataRes;

    try {
      if (checkRes) {
        let res = await service.blog.classify.update();
        if (res == '您不是管理员') {
          return ctx.helper.errorModule(res);
        } else {
          return ctx.helper.successModule('修改成功');
        }
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('操作失败');
    }
  }
}
module.exports = ClassifyController;
